/**
 * 登录模块，提供登录相关业务功能
 * 
 * @Date: 2015-6-16
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery", "datagridServ","sysmanMain","msgServ", "ajaxServ", "formServ", "bizProto","deptSelect"],
		function($,datagridServ,sysmanMain, msgServ, ajaxServ, formServ, bizProto,deptSelect) {

			'use strict';

			var _bizProto,_buttonConfig,
				//锚记datagrid目标元素
				$dg,
				//初始化后的datagrid dom对象
				$grid,
				//编辑弹窗对象及弹窗按钮对象
				$edit_window,window_btn,
				//设置密码弹窗对象及按钮对象
				$password_window,password_btn,
				//业务表单对象
				$form,
				//部门选择框字段
				$deptNameField, $deptIdField,formEl, formData = 0, 
				 
				roleSaveUrl = '/roleSave.action', 	baseSaveUrl	,
				
				//岗位角色编辑页面url
				jobroleEditUrl = '/jobrole/query/editPage.action',
				
				//编辑页面初始化设置
				window_width = 600,
				window_height= 400,
				
				//编辑页面传递参数
				openParam,
				
				rootid = 'panel-roleAssign',
				//编辑窗口dom id
				edit_window_id='role-edit-win'
				

			var public_method = {
				
				
				init:function(doc){
					private_method.init(doc);
				},
				
				
				//初始化编辑页面
				initEdit:function(_options){
					private_method.initEdit(_options)
				},
				/**
				 * 打开编辑页面
				 * @param _options.data	选中的数据行
				 */
				openPage:function(_options){
					var rootpath = '/'+sysmanMain.getRootPath();
					var _config ={
						width:window_width,
						height:window_height,
						window:$('#'+edit_window_id),
						grid:$('#'+rootid + ' div.dg-role'),
						href:rootpath+jobroleEditUrl,
						flag:false,
						existCheckElement:'#div-dept-user',
						onClose:private_method.onClose
					}					
							
					var _param = $.extend({},_config,_options);
					openParam = _param;
					datagridServ.windowEdit(_param);			
				
				}
				
			}

			var private_method = {
				
				/**
				 * 编辑页面初始化
				 */
				initEdit:function(_options){
					if(openParam && openParam.data){
						$('#'+edit_window_id + ' form.main-form').form('load',openParam.data)
					}
					var rootpath = '/'+sysmanMain.getRootPath();
					
					//部门选择
					var _el = $('#'+edit_window_id+' #deptname')
					if(_el &&_el.length>0){
						
						$(_el).textbox({
							onClickButton:function(data){
								var _userParam = {									
									deptid:edit_window_id +' #deptid',
									deptname:edit_window_id+' #deptname',
									selectType:'job',
									subsystem:'sysman'
								}
								deptSelect.openPage(_userParam)							
							}
						})
					}
					//初始化角色下拉选框
					var jobroleEl = $('#'+edit_window_id + ' #jobcode')
					if(jobroleEl.length ==1){
						$(jobroleEl).combobox({
							textField:'jobname',
							valueField:'jobcode',
							url:rootpath + '/jobrole/query/getTypeListAll.action',
							onSelect:function(record){
								$('#'+edit_window_id +' #jobname').val(record.jobname)								
							}
						})
					}
				},
				
				
				
				//初始化元素
				initElement:function(doc){
					
					
					$grid = $(doc).find("#dg-rolePermission-role");
					
					// 编辑窗口初始化
					$edit_window = $('#roleEdit-win');
					window_btn = $($edit_window).find('a.function-btn')
					$(window_btn).unbind('click')
					$(window_btn).bind('click',	private_method.function_btn_listenner)
					$form = $($edit_window).find('form')					
					
					var selectedRecord = $grid.datagrid('getSelected');
					
					if(selectedRecord.parentid==null)						
						selectedRecord.parentid='';
					var addFlag = $($edit_window).attr('flag');
					if(addFlag==null)
						$form.form('load',selectedRecord);

				},				
				
				
				onClose:function(){
					openParam = undefined;
					$('#'+edit_window_id).empty();
				}
			};
			
			return public_method;

		});